package com.rzdata.poct.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rzdata.poct.common.core.mybatisplus.core.ServicePlusImpl;
import com.rzdata.poct.common.core.page.PagePlus;
import com.rzdata.poct.common.core.page.TableDataInfo;
import com.rzdata.poct.common.utils.PageUtils;
import com.rzdata.poct.common.utils.StringUtils;
import com.rzdata.poct.system.domain.business.bo.PoctInstrumentLogsBo;
import com.rzdata.poct.system.domain.business.po.PoctInstrumentLogs;
import com.rzdata.poct.system.domain.business.vo.PoctInstrumentLogsVo;
import com.rzdata.poct.system.mapper.PoctInstrumentLogsMapper;
import com.rzdata.poct.system.service.IPoctInstrumentLogsService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * POCT设备操作Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-09
 */
@Service
public class PoctInstrumentLogsServiceImpl extends ServicePlusImpl<PoctInstrumentLogsMapper, PoctInstrumentLogs, PoctInstrumentLogsVo> implements IPoctInstrumentLogsService {

    @Override
    public PoctInstrumentLogsVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PoctInstrumentLogsVo> queryPageList(PoctInstrumentLogsBo bo) {
        PagePlus<PoctInstrumentLogs, PoctInstrumentLogsVo> result =
            pageVo(PageUtils.buildPagePlusNew(Long.valueOf(bo.getPageNum()), Long.valueOf(bo.getPageSize())), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<PoctInstrumentLogsVo> queryList(PoctInstrumentLogsBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<PoctInstrumentLogs> buildQueryWrapper(PoctInstrumentLogsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoctInstrumentLogs> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getInstrumentName()), PoctInstrumentLogs::getInstrumentName, bo.getInstrumentName());
        lqw.like(StringUtils.isNotBlank(bo.getInstrumentNo()), PoctInstrumentLogs::getInstrumentNo, bo.getInstrumentNo());
        lqw.eq(bo.getOptType() != null, PoctInstrumentLogs::getOptType, bo.getOptType());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), PoctInstrumentLogs::getOperator, bo.getOperator());
        lqw.eq(bo.getOptTime() != null, PoctInstrumentLogs::getOptTime, bo.getOptTime());
        return lqw;
    }

    @Override
    public Boolean insertByBo(PoctInstrumentLogsBo bo) {
        PoctInstrumentLogs add = BeanUtil.toBean(bo, PoctInstrumentLogs.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByBo(PoctInstrumentLogsBo bo) {
        PoctInstrumentLogs update = BeanUtil.toBean(bo, PoctInstrumentLogs.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PoctInstrumentLogs entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
